//
//  TMCourseSaleController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/7/5.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMCourseSaleController.h"
#import "TMCourseSaleCell.h"
#import "TMCourseHeaderView.h"
#import "TMCommodityDetailController.h"
#import "TMNearbyViewModel.h"
#import "TMShopCourseModel.h"
#import "TMCourseTypeController.h"
@interface TMCourseSaleController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *courseCollection;
@property (nonatomic, assign) CGSize itemSize;
@property (nonatomic, strong) TMCourseHeaderView *headerView ;
@property (nonatomic, strong) TMNearbyViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray *typeList;
@property (nonatomic, strong) NSMutableArray *courseList;
@property (nonatomic, copy) NSString *nameStr;
@property (nonatomic, strong) WZNoResultView *noResultView;
@property (nonatomic, assign) NSInteger tid;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalNum;
@end

@implementation TMCourseSaleController
- (NSMutableArray *)typeList{
    if (!_typeList) {
        _typeList = [NSMutableArray array];
    }
    return _typeList;
}
- (NSMutableArray *)courseList{
    if (!_courseList) {
        _courseList = [NSMutableArray array];
    }
    return _courseList;
}
- (void)setCourseCollection:(UICollectionView *)courseCollection{
    _courseCollection = courseCollection;
    _courseCollection.delegate = self;
    _courseCollection.dataSource = self;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 12.;
    flowLayout.minimumInteritemSpacing = 0.;
    _itemSize = CGSizeMake(173 * kWindowW /375, 246 * kWindowW /375);
    _courseCollection.collectionViewLayout = flowLayout;
    [_courseCollection registerNib:[UINib nibWithNibName:@"TMCourseSaleCell" bundle:nil] forCellWithReuseIdentifier:@"TMCourseSaleCell"];
    [_courseCollection registerClass:[TMCourseHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TMCourseHeaderView"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程出售";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTypeCourse:) name:@"CHOOSECOURSETYPENOTIFICATION" object:nil];
    self.viewModel = [[TMNearbyViewModel alloc]init];
    if (!self.tid) {
        self.tid = 10000;
    }
    self.courseCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //self.cid
        self.pageNum = 1;
        [self.viewModel getShopCourseSaleWithParam:self.cid Type:self.tid Page:self.pageNum  Success:^(id responseObject,id respon) {
            self.typeList = responseObject[0];
            self.courseList = responseObject[1];
            self.totalNum = [respon integerValue];
            [self noResultViewShowORNot];
            [self.courseCollection reloadData];
            [self.courseCollection.mj_header endRefreshing];
            [self.courseCollection.mj_footer endRefreshing];
        } Failed:^(NSString *error) {
            [self noResultViewShowORNot];
            [self.courseCollection reloadData];
            [self.courseCollection.mj_header endRefreshing];
             [self.courseCollection.mj_footer endRefreshing];
        }];
    }];
    self.courseCollection.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.pageNum ++;
        if (self.pageNum > self.totalNum) {
            [self.courseCollection.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        [self.viewModel getShopCourseSaleWithParam:self.cid Type:self.tid Page:self.pageNum  Success:^(id responseObject,id respon) {
            self.typeList = responseObject[0];
            self.courseList = responseObject[1];
            [self.courseCollection reloadData];
            [self.courseCollection.mj_header endRefreshing];
            [self.courseCollection.mj_footer endRefreshing];
        } Failed:^(NSString *error) {
            if (self.pageNum > 1) {
                self.pageNum--;
            }
            [self.courseCollection reloadData];
            [self.courseCollection.mj_header endRefreshing];
            [self.courseCollection.mj_footer endRefreshing];
        }];
    }];
    [self.courseCollection.mj_header beginRefreshing];
}
- (void)getTypeCourse:(NSNotification *)noti{
    self.tid = [noti.userInfo[@"tyid"]integerValue];
    self.nameStr = noti.userInfo[@"name"];
    [self.headerView.titleBtn setTitle:self.nameStr forState:UIControlStateNormal];
    [self.courseCollection.mj_header beginRefreshing];
}


#pragma delegate datas

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return self.itemSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(7, 12, 0, 12);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.courseList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TMCourseSaleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TMCourseSaleCell" forIndexPath:indexPath];
    cell.model = self.courseList[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TMCommodityDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCommodityDetailController"];
    vc.isDiscount = NO;
    TMShopCourseModel *model = self.courseList[indexPath.row];
    vc.rid = model.hcid;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -  header
//要先设置表头高度
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
   // CGSize size = {kWindowW, 252*kWindowW/375};
    CGSize size = {kWindowW, 50};
    return size;
}

//在表头内添加内容,需要创建一个继承collectionReusableView的类,用法类比tableViewcell
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 初始化表头
    _headerView = (TMCourseHeaderView*)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TMCourseHeaderView" forIndexPath:indexPath];
    __weak typeof(self) weakself = self;
    _headerView.buyDiscount = ^{
        TMCommodityDetailController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCommodityDetailController"];
        vc.isDiscount = YES;
        [weakself.navigationController pushViewController:vc animated:YES];
    };
    _headerView.chooseBlock = ^{
        TMCourseTypeController *vc = [[UIStoryboard storyboardWithName:@"Nearby" bundle:nil] instantiateViewControllerWithIdentifier:@"TMCourseTypeController"];
        vc.courseList = weakself.typeList;
        [weakself.navigationController pushViewController:vc animated:YES];
    };
    return _headerView;
}
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (kWindowH == 812.0) {
            _noResultView.frame = CGRectMake(0, 50, kWindowW, kWindowH-50);
        }else {
            _noResultView.frame = CGRectMake(0, 50, kWindowW, kWindowH-50);
        }
        _noResultView.isSearchVC = YES;
        [self.courseCollection addSubview:_noResultView];
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.courseList.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}
@end
